trigger DiscussionForumBeforeDelete on DiscussionForum__c bulk (before delete) {
	if (!TeamUtil.currentlyExeTrigger) {
        try{
            TeamUtil.currentlyExeTrigger = true;
            
            List<String> idsForum = new List<String>();
            for (DiscussionForum__c iterForum : Trigger.old) {
                idsForum.add(iterForum.Id); 
            }
            
            List<DiscussionTopic__c> topicsList = [select Id, Name, DiscussionForum__c from DiscussionTopic__c where DiscussionForum__c in : idsForum ];
            List<DiscussionMember__c> membersList = [select Id, Name, DiscussionForum__c from DiscussionMember__c where DiscussionForum__c in : idsForum ];
            
            delete topicsList;
            delete membersList;
            


            // Deleting Groups y Group Members
            String grName = 'DiscussionForum' + idsForum[ 0 ];
            
            List<String> grs = new List<String>(); 
            for ( Group gr : [ SELECT Id FROM Group WHERE Name =: grName ])
            grs.add( gr.id );
            
            List<String> grMembers = new List<String>();
            for( GroupMember gm : [ SELECT Id FROM GroupMember WHERE GroupId in: grs ])
            grMembers.add( gm.id );
        
        	TeamUtil.deleteGroupMembers( grMembers );
        	TeamUtil.deleteGroup( grs );

        	
        } finally {
            TeamUtil.currentlyExeTrigger = false;
        }
    }
}